unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzEdit, ExtCtrls, RzPanel, RzButton, rtcInfo, rtcConn,
  rtcDataCli, rtcHttpCli,PerlRegEx, rtcDataSrv, RzStatus,Dialog_View;

type
  TForm1 = class(TDialogView)
    Tool_1: TRzToolbar;
    Memo_1: TRzMemo;
    Btnx_HTTP: TRzBitBtn;
    Btnx_Exit: TRzBitBtn;
    RtcHttpClient1: TRtcHttpClient;
    RtcDataRequest1: TRtcDataRequest;
    Btnx_Parse: TRzBitBtn;
    Tool_2: TRzStatusBar;
    Prog_1: TRzProgressStatus;
    procedure Btnx_HTTPClick(Sender: TObject);
    procedure RtcDataRequest1DataReceived(Sender: TRtcConnection);
    procedure Btnx_ExitClick(Sender: TObject);
    procedure RtcDataRequest1BeginRequest(Sender: TRtcConnection);
    procedure RtcHttpClient1ConnectError(Sender: TRtcConnection;
      E: Exception);
    procedure RtcHttpClient1ConnectFail(Sender: TRtcConnection);
    procedure RtcHttpClient1Connecting(Sender: TRtcConnection);
    procedure RtcHttpClient1Connect(Sender: TRtcConnection);
    procedure RtcHttpClient1ConnectLost(Sender: TRtcConnection);
    procedure Btnx_ParseClick(Sender: TObject);
    procedure RtcDataRequest1DataIn(Sender: TRtcConnection);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,Class_KzDebug;

{$R *.dfm}

procedure TForm1.Btnx_HTTPClick(Sender: TObject);
begin
  {#with RtcHttpClient1 do
  begin
    ServerAddr:='www.bbc.co.uk';
    ServerPort:='80';
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.Method  :='GET';
    Request.FileName:='/worldservice/learningenglish/general/sixminute/2014/07/140710_6min_brazilian_economy.shtml';
    Post();
  end;}

  {#//http://www.baidu.com/img/bd_logo1.png
  with RtcHttpClient1 do
  begin
    ServerAddr:='www.baidu.com';
    ServerPort:='80';
    Connect();
  end;
  with RtcDataRequest1 do
  begin
    Request.Method  :='GET';
    Request.FileName:='/img/bd_logo1.png';
    Post();
  end;}

  {#//http://b.zol-img.com.cn/desk/bizhi/image/4/960x600/1391737541348.jpg
  with RtcHttpClient1 do
  begin
    ServerAddr:='b.zol-img.com.cn';
    ServerPort:='80';
    Connect();
  end;
  with RtcDataRequest1 do
  begin
    Request.Method  :='GET';
    Request.FileName:='/desk/bizhi/image/4/960x600/1391737541348.jpg';
    Post();
  end;}

  //http://www.baidu.com/img/bd_logo1.png
  with RtcHttpClient1 do
  begin
    ServerAddr:='192.168.0.34';
    ServerPort:='8080';
    Connect();
  end;
  with RtcDataRequest1 do
  begin
    Request.Method  :='GET';
    Request.FileName:='/update/PRMSX/PRMSX.exe';
    Post();
  end;
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
var
  s:RtcString;
  RtcDataClient:TRtcDataClient absolute Sender;
begin
  with RtcDataClient do
  begin
    if Response.Started then
    begin
      if not DirectoryExists('download') then
      begin
        CreateDir('download');
      end;
      self.Prog_1.TotalParts:=Response.ContentLength;
      Self.Memo_1.Lines.Add(Response.ContentType);
      Self.Memo_1.Lines.Add(Format('%D',[Response.DataSize]));
    end;

    S:=Read;
    Write_File('download\ZCGL.zip', s, Response.ContentIn-length(s));

    self.Prog_1.PartsComplete:=Response.ContentIn;

    if Response.Done then
    begin
      self.Prog_1.TotalParts   :=0;
      self.Prog_1.PartsComplete:=0;
    end;
  end;
end;

procedure TForm1.Btnx_ExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
begin
  with TRtcDataClient(Sender) do
  begin
    if Request.Host = '' then
    begin
      Request.Host := ServerAddr;
      Request.Agent :='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)';
    end;
    Memo_1.Lines.Clear;
    WriteHeader;
  end;
end;

procedure TForm1.RtcHttpClient1ConnectError(Sender: TRtcConnection;
  E: Exception);
begin
  //
  Memo_1.Lines.Add(Format('%S:%S',['RtcHttpClient1ConnectError',E.Message]));
  KzDebug.FileFmt('%S:%S',['RtcHttpClient1ConnectError',E.Message]);
end;

procedure TForm1.RtcHttpClient1ConnectFail(Sender: TRtcConnection);
begin
  //
  Memo_1.Lines.Add('RtcHttpClient1ConnectFail');
  KzDebug.FileLog('RtcHttpClient1ConnectFail');
end;

procedure TForm1.RtcHttpClient1Connecting(Sender: TRtcConnection);
begin
  //
  Memo_1.Lines.Add('RtcHttpClient1Connecting');
  KzDebug.FileLog('RtcHttpClient1Connecting');
end;

procedure TForm1.RtcHttpClient1Connect(Sender: TRtcConnection);
begin
  //
  Memo_1.Lines.Add('RtcHttpClient1Connect');
  KzDebug.FileLog('RtcHttpClient1Connect');
end;

procedure TForm1.RtcHttpClient1ConnectLost(Sender: TRtcConnection);
begin
  //
  Memo_1.Lines.Add('RtcHttpClient1ConnectLost');
  KzDebug.FileLog('RtcHttpClient1ConnectLost');
end;

procedure TForm1.Btnx_ParseClick(Sender: TObject);
var
  PerlA:TPerlRegEx;
begin
  try
    PerlA:=TPerlRegEx.Create;
    //PerlA.Subject:='http://http-ws.bbc.co.uk.edgesuite.net/mp3/learningenglish/2014/07/bbc_6min_brazilian_economy_web_140710_6min_brazilian_economy_audio_au_bb.mp3';
    PerlA.Subject:=Memo_1.Lines.Text;
    PerlA.RegEx  :='http:\/\/.*\.mp3';
    while PerlA.MatchAgain do
    begin
      ShowMessageFmt('Postion:%D'+#13+'%S',[PerlA.MatchedOffset,PerlA.MatchedText]);
      KzDebug.FileFmt('Postion:%D'+#13+'%S',[PerlA.MatchedOffset,PerlA.MatchedText]);
    end;
  finally
    FreeAndNil(PerlA);
  end;
end;

procedure TForm1.RtcDataRequest1DataIn(Sender: TRtcConnection);
  var
    bSize:int64;
    s:RtcString;
    RtcDataClient:TRtcDataClient absolute Sender;
  begin
  with RtcDataClient do
    begin
      if Request.ContentLength>Request.ContentOut then
      begin
      bSize:=Request.ContentLength-Request.ContentOut;
      if bSize>64000 then bSize:=64000;
//      Write(Read_File(Request.Info.asText['file'], Request.ContentOut, bSize));
      s:=Read;
      Write_File('0001.png', s, Request.ContentOut-length(s));
      end;
    end;
  end;

end.
